package content

import (
	"context"

	"server-go/api/reward/internal/svc"
	"server-go/api/reward/internal/types"
	"server-go/rpc/reward/types/reward"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetContentLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetContentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContentLogic {
	return &GetContentLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetContentLogic) GetContent(req *types.GetContentInput) (resp *types.GetContentOutput, err error) {
	resp = &types.GetContentOutput{}
	// 兼容输入的页码和页大小
	if req.PageNo <= 0 {
		req.PageNo = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 10
	}

	res, err := l.svcCtx.RewardRpc.GetContent(l.ctx, &reward.GetContentInput{
		PageNo:      req.PageNo,
		PageSize:    req.PageSize,
		SearchValue: req.SearchValue,
		TaskTypeId:  req.TaskTypeId,
	})

	if err != nil {
		return resp, err
	}
	for _, v := range res.ContentData {
		resp.ContentData = append(resp.ContentData, &types.GetContent{
			Avatar:      v.Avatar,
			Bounty:      v.Bounty,
			Content:     v.Content,
			Description: v.Description,
			EndDate:     v.Enddate,
			Id:          v.Id,
			NickName:    v.Nickname,
			Title:       v.Title,
			UserIntro:   v.Userintro,
		})
	}
	resp.Total = res.Total
	return resp, nil
}
